package com.devmindset.legoframework.usage.client.component;

import java.util.List;

import com.devmindset.legoframework.client.mvc.LegoComponent;
import com.devmindset.legoframework.client.mvc.LegoModel;
import com.devmindset.legoframework.client.mvc.metadata.ComponentStory;
import com.devmindset.legoframework.client.mvc.metadata.Stories;
import com.devmindset.legoframework.usage.client.constants.UsageConstants;
import com.devmindset.legoframework.usage.client.event.Events;
import com.devmindset.legoframework.usage.client.util.BannerInfo;
import com.devmindset.legoframework.usage.client.util.Util;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLFlow;

public class BannerComponent extends LegoComponent {

	/** {@inheritDoc} */
	@Override
	public Canvas construct(LegoModel model) {
		List<BannerInfo> banners = Util.getBanners();
		
		StringBuffer sb = new StringBuffer();
		sb.append("<table width=\"100%\">");
		for(BannerInfo bannerInfo : banners) {
			sb.append("<tr width=\"100%\">");
			sb.append("<td height=\"" + bannerInfo.getHeight() + "\" style=\"border: 1px solid #A6ABB4; background-color: white\"><center><a href=\"" + bannerInfo.getUrl() + "\" target=\"_blank\"><img src=\"images/banners/" + bannerInfo.getImageFile() + "\" border=\"0\" width=\"" + bannerInfo.getWidth() + "\" height=\"" + bannerInfo.getHeight() + "\"/></a></center></td>");
			sb.append("</tr>");
		}
		sb.append("</table>");
		
		HTMLFlow flow = new HTMLFlow(sb.toString());
		flow.setBackgroundColor(UsageConstants.COMPONENT_BACKGROUND);
		flow.setHeight100();
		flow.setWidth100();
		return flow;
	}
	
	/** {@inheritDoc} */
	@Override
	public void calculate(LegoModel model) {
		//nothing to additionally calculate for this component
	}
	
	/** {@inheritDoc} */
	public ComponentStory myStory() {
		return Stories.component()
			.constructedBy(Events.RENDER_BANNER_COMPONENT)
			.knownAs("Banner Component");
	}

}